package com.hzit.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.common.utils.SmsUtil;
import com.hzit.user.config.SmsProperties;
import com.hzit.user.dao.UserDao;
import com.hzit.user.entity.User;
import com.hzit.user.service.UserSerice;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.print.attribute.HashAttributeSet;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
@Aspect
public class UserServiceImpl implements UserSerice {
	@Autowired
	private StringRedisTemplate redisTemplate;
	@Autowired
	private RocketMQTemplate rocketMQTemplate;
	@Autowired
	private SmsProperties properties;
	@Autowired
	private UserDao userDao;


	//1. 发送手机验证码
	@Override
	public void sendCode(String phone) {
		//1.1 得到验证码
		String code = RandomUtil.randomNumbers(4);
		//1.2 保存到redis中
		redisTemplate.opsForValue().set(phone,code);
		//1.3 将发送验证码需要的一系列信息包装成map并发送出去
		//1.3.1 构造要发送的信息map集合
		//  setTemplateParam("{\"code\":\"1234\"}");
		Map map = new HashMap<>();
		map.put("signName",properties.getSignName());
		map.put("templateCode",properties.getTemplateCode());
		map.put("accessKeyId",properties.getAccessKeyId());
		map.put("accessKeySecret",properties.getAccessKeySecret());
		map.put("phone",phone);
		Map codeMap = new HashMap();
		codeMap.put("code",code);
		map.put("templateParam", JSON.toJSONString(codeMap));
		//1.3.2 发送数据
		rocketMQTemplate.convertAndSend("topic-sms",map);
	}

	//2. 校验验证码是否与redis中的一致
	@Override
	public boolean validCode(String code,String phone) {
		//2.1 从redis中得到验证码
		String redisCode = redisTemplate.opsForValue().get(phone);
		//2.2 与输入的验证码比对
		// if(StrUtil.isNotBlank(phone) && code.equals(redisCode)){
		// 	return true;
		// }
		// return false;

		return StrUtil.isNotBlank(phone) && code.equals(redisCode);
	}

	//3. 添加用户
	@Override
	public void add(User user) {
		user.setCreated(new Date());
		user.setUpdated(new Date());
		// 将密码进行md5加密
		String password = user.getPassword();
		password = SecureUtil.md5(password);
		user.setPassword(password);
		userDao.insert(user);
	}

	@Override
	public List<User> findAll() {
		return userDao.selectList(null);
	}

	//3. 根据用户id查询用户
	@Override
	public User findById(String id) {
		return this.userDao.selectById(id);
	}

	@Transactional
	public void a(){
		UserServiceImpl userSerice = (UserServiceImpl) AopContext.currentProxy();

		userSerice.b();
		userSerice.c();
	}
	//Aop: 代理
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void b(){

	}

	@Transactional
	public void c(){

	}
}
